﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace DAL
{

    public sealed class DbHelper2000
    {
        public static string strConn;

        static DbHelper2000()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["duanxinDb"].ConnectionString;
        }

        #region exec procedure
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sel_list"></param>
        /// <param name="table_name"></param>
        /// <param name="where"></param>
        /// <param name="primary"></param>
        /// <param name="order_by"></param>
        /// <param name="page_size"></param>
        /// <param name="page_index"></param>		
        /// <returns></returns>
        public static DataSet ExecuteDataSetPageSize(string sel_list, string table_name, string where, string primary, string order_by, int page_size, int page_index)
        {
            SqlParameter[] parameterArr = new SqlParameter[7];
            parameterArr[0] = new SqlParameter("@select_list", SqlDbType.VarChar, 1000);
            parameterArr[0].Value = sel_list;
            parameterArr[1] = new SqlParameter("@table_name", SqlDbType.VarChar, 500);
            parameterArr[1].Value = table_name;
            parameterArr[2] = new SqlParameter("@where", SqlDbType.VarChar, 1000);
            parameterArr[2].Value = where;
            parameterArr[3] = new SqlParameter("@primary_key", SqlDbType.VarChar, 100);
            parameterArr[3].Value = primary;
            parameterArr[4] = new SqlParameter("@order_by", SqlDbType.VarChar, 200);
            parameterArr[4].Value = order_by;
            parameterArr[5] = new SqlParameter("@page_size", SqlDbType.SmallInt);
            parameterArr[5].Value = page_size;
            parameterArr[6] = new SqlParameter("@page_index", SqlDbType.Int);
            parameterArr[6].Value = page_index;
            return SqlHelper.ExecuteDataset(strConn, "P_select_pageSize", parameterArr);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <param name="parameters"></param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteDataTable(string sqlSpName, params object[] parameters)
        {
            return SqlHelper.ExecuteDataset(strConn, sqlSpName, parameters).Tables[0];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataSet ExecuteReDataSet(string sqlSpName, params object[] parameters)
        {
            return SqlHelper.ExecuteDataset(strConn, sqlSpName, parameters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <returns>DataTable</returns>
        public static DataTable ExecuteDataTable(string sqlSpName)
        {
            return ExecuteDataTable(sqlSpName, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <param name="parameters"></param>
        /// <returns>DataRow</returns>
        public static DataRow ExecuteDataRow(string sqlSpName, params object[] parameters)
        {
            DataTable dt = ExecuteDataTable(sqlSpName, parameters);

            if (dt.Rows.Count > 0)
                return dt.Rows[0];
            else
                return null;
        }

        /// <summary>
        ///  
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <returns>DataRow</returns>
        public static DataRow ExecuteDataRow(string sqlSpName)
        {
            return ExecuteDataRow(sqlSpName, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <param name="parameters"></param>
        public static void ExecuteNonQuery(string sqlSpName, params object[] parameters)
        {
            SqlHelper.ExecuteNonQuery(strConn, sqlSpName, parameters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        public static void ExecuteNonQuery(string sqlSpName)
        {
            ExecuteNonQuery(sqlSpName, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sqlSpName, params object[] parameters)
        {
            return SqlHelper.ExecuteScalar(strConn, sqlSpName, parameters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sqlSpName)
        {
            return ExecuteScalar(sqlSpName, null);
        }

        #endregion

        #region exec sql

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        public static DataSet ExecuteSqlDataSet(string cmdText)
        {
            return SqlHelper.ExecuteDataset(strConn, CommandType.Text, cmdText);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <returns></returns>
        public static DataTable ExecuteSqlDataTable(string cmdText)
        {
            return SqlHelper.ExecuteDataset(strConn, CommandType.Text, cmdText).Tables[0];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataTable ExecuteSqlDataTable(string cmdText, params SqlParameter[] parameters)
        {
            return SqlHelper.ExecuteDataset(strConn, CommandType.Text, cmdText, parameters).Tables[0];
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static DataRow ExecuteSqlDataRow(string sql, params SqlParameter[] parameters)
        {
            DataTable dt = ExecuteSqlDataTable(sql, parameters);

            if (dt.Rows.Count > 0)
                return dt.Rows[0];
            else
                return null;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sqlSpName"></param>
        /// <returns></returns>
        public static DataRow ExecuteSqlDataRow(string sqlSpName)
        {
            return ExecuteSqlDataRow(sqlSpName, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        public static void ExecuteSqlNonQuery(string sql, params SqlParameter[] parameters)
        {
            SqlHelper.ExecuteNonQuery(strConn, CommandType.Text, sql, parameters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        public static void ExecuteSqlNonQuery(string sql)
        {
            ExecuteSqlNonQuery(sql, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static object ExecuteSqlScalar(string sql, params SqlParameter[] parameters)
        {
            return SqlHelper.ExecuteScalar(strConn, CommandType.Text, sql, parameters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static object ExecuteSqlScalar(string sql)
        {
            return ExecuteSqlScalar(sql, null);
        }
        #endregion

    }
}
